package com.rd.student.ruanjian.class1.duhang.Week04.New01;

import java.util.Scanner;

public class PrintNumber {

    public static void main(String[] args) throws InterruptedException {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入数字：");
        int num = scanner.nextInt();
        ZeroEvenOdd ze = new ZeroEvenOdd(num);
        new Thread(ze::zero).start();
        new Thread(ze::even).join();
        new Thread(ze::even).start();
        new Thread(ze::odd).join();
        new Thread(ze::odd).start();
    }
}

class ZeroEvenOdd extends Thread{
    int n;

    public ZeroEvenOdd(int n) {
        this.n = n;
    }

    public synchronized void zero() {
        //输出0
        for (int i = 0; i <n; i++) {
            System.out.print(0);
            notify();
            try {
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public synchronized void even() {
        //输出偶数
        for (int i = 1; i <= n; i++) {
            if (i % 2 == 0){
                System.out.print(i);
                notify();
                try {
                    wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public synchronized void odd() {
        //输出奇数
        for (int i = 1; i <= n; i++) {
            if (i % 2 != 0){
                System.out.print(i);
                notify();
                try {
                    wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }


}
